package com.easyfun.healthmagicbox;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import cn.kuaipan.android.openapi.KuaiPanManager;

import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.base.PicturePickerBaseActivity;
import com.easyfun.healthmagicbox.comm.HealthThreadPool;
import com.easyfun.healthmagicbox.comm.PersonProfileUploadTask;
import com.easyfun.healthmagicbox.comm.UserToPersonsUpTask;
import com.easyfun.healthmagicbox.inputviews.DoctorSearchActivity;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.HealthInputData;
import com.easyfun.healthmagicbox.pojo.PersonProfile;
import com.easyfun.healthmagicbox.pojo.UserToPersons;
import com.easyfun.healthmagicbox.sync.BasicCommCallback;
import com.easyfun.healthmagicbox.sync.CommCallback;
import com.easyfun.healthmagicbox.util.HealthDateUtil;
import com.easyfun.healthmagicbox.util.ImageTools;
import com.easyfun.healthmagicbox.util.PersonProfileCache;
import com.easyfun.healthmagicbox.util.URIConverter;
import com.easyfun.healthmagicbox.widget.CustomDialogUtil;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.IBinder;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Shader.TileMode;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class ProfileActivity extends PicturePickerBaseActivity implements DatabaseAccess {

	private final String LOG_TAG = getClass().getSimpleName();

	
	private static final int SCALE = 8;//照片缩小比例
	private ImageView iv_image = null;
	
	//private String m_imageFileName = ConstantData.NoPicture;
	private ArrayAdapter<String> titleadapter;
	private String mSex = "男";

	private Spinner spinnerTitle;


	public String mTitle="自己";
	public String mOldTitle ="";
	
	EditText txtName = null;
	EditText txtWeight = null;
	EditText txtHeight= null;
	RadioGroup radioGroupsex = null;
	RadioButton radioMale;
	RadioButton radioFemale;
	int mYear = 1980;  
    int mMonth = 1;
    int mDay = 1;
	private EditText sellStartTime;  
	boolean takeAPhoto = false;
	String imageFileNameInOld = "";
	String mSharelist = "";
	
	EditText txtAllerge = null;
	EditText txtDisease= null;
	EditText txtLife = null;
	EditText txtEat= null;
	EditText txtIssues = null;
	
	
	CustomDialogUtil allergeDiag;
	CustomDialogUtil diseaseDiag;
	CustomDialogUtil lifeDiag;
	CustomDialogUtil eatDiag;
	CustomDialogUtil issuesDiag;
	CustomDialogUtil selectedDialog;
	
	Map<EditText,CustomDialogUtil> editInputMap = new HashMap<EditText,CustomDialogUtil>();
	class MySelectBoxClick implements OnClickListener {
		EditText edittext = null;
		MySelectBoxClick(EditText edittext )
		{
			this.edittext = edittext;
		}
		@Override
		public void onClick(View v) {
			selectedDialog = editInputMap.get(edittext);
			selectedDialog.createDialog();	
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_profile);
		//should after set Content
		addbackButton(this);
		checkPersonId();
		//self can't change title
		
		txtName = (EditText) findViewById(R.id.txtName);
		
		sellStartTime = (EditText) findViewById(R.id.txtOld);
		
		txtWeight = (EditText) findViewById(R.id.txtWeight);
		txtHeight = (EditText) findViewById(R.id.txtHeight);
		
		//-------------------------------------------------------
		String dialogTitle;
		txtAllerge = (EditText) findViewById(R.id.txtAllerge);
		
		dialogTitle = (String)((TextView)findViewById(R.id.allegic_history)).getText();
		allergeDiag = new CustomDialogUtil(ProfileActivity.this, 
				ConstantData.allerges,txtAllerge, (String)((TextView)findViewById(R.id.allegic_history)).getText());
		editInputMap.put(txtAllerge, allergeDiag);
		
		txtDisease = (EditText) findViewById(R.id.txtDisease);
		diseaseDiag = new CustomDialogUtil(ProfileActivity.this, 
				ConstantData.diseases,txtDisease, (String)((TextView)findViewById(R.id.chronic_illness)).getText());
		editInputMap.put(txtDisease, diseaseDiag);
		
		txtLife = (EditText) findViewById(R.id.txtLife);
		lifeDiag = new CustomDialogUtil(ProfileActivity.this, 
				ConstantData.life,txtLife, (String)((TextView)findViewById(R.id.living_habit)).getText());
		editInputMap.put(txtLife, lifeDiag);
		
		txtEat= (EditText) findViewById(R.id.txtEat);
		eatDiag = new CustomDialogUtil(ProfileActivity.this, 
				ConstantData.eat,txtEat,  (String)((TextView)findViewById(R.id.eating_habit)).getText());
		editInputMap.put(txtEat, eatDiag);
		
		txtIssues = (EditText) findViewById(R.id.txtIssues);
		issuesDiag = new CustomDialogUtil(ProfileActivity.this, 
				ConstantData.bodyissues,txtIssues, (String)((TextView)findViewById(R.id.body_problems)).getText());
		editInputMap.put(txtIssues, issuesDiag);
		
		for (EditText edittext : editInputMap.keySet()) {
			edittext.setOnClickListener(new MySelectBoxClick(edittext));
		}
		//---------------------------------------------------------
		
		radioMale = (RadioButton) findViewById(R.id.radioMale);
		radioFemale = (RadioButton) findViewById(R.id.radioFemale);
		
		radioGroupsex = (RadioGroup) findViewById(R.id.radioSex);
		radioMale.setChecked(true);
		radioFemale.setChecked(false);
		
		spinnerTitle = (Spinner) findViewById(R.id.spinnerTitle);
		radioMale.setChecked(true);
		radioFemale.setChecked(false);
		
		if (m_personid.equals(PersonProfileCache.getInstance(ProfileActivity.this)
				.getPersonID(ProfileActivity.this)))
		{
			spinnerTitle.setEnabled(false);
		}
		iv_image = (ImageView) this.findViewById(R.id.img);

		//create profile
		this.findViewById(R.id.btn_submit).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				saveProfile(ProfileActivity.this);
				Toast.makeText(ProfileActivity.this, "保存", Toast.LENGTH_LONG).show();
				//final Intent intent = new Intent(ProfileActivity.this, HMBMainActivity.class);	
				finish();
				//go back main page.
				//startActivity(intent);//
			}
		});
		
		//pick up a picture
		this.findViewById(R.id.btn_picture).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showPicturePicker(ProfileActivity.this);
			}
		});

		
		radioGroupsex.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.radioFemale:
					mSex = "女";
					break;
				case R.id.radioMale:
					mSex = "男";
					break;
				}
			}

		});
		
		
		// 将可选内容与ArrayAdapter连接起来
		titleadapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, getAllTitle());
		spinnerTitle.setAdapter(titleadapter);
		spinnerTitle.setOnItemSelectedListener(new OnItemSelectedListener()
		{

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				mTitle =  ConstantData.titles.get(position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
			
		});
		sellStartTime.setOnClickListener(new OnClickListener() {  			
			@Override
			public void onClick(View v) {
				
				 new DatePickerDialog(ProfileActivity.this,  
		                    mDateSetListener,  
		                    mYear, mMonth, 
		                    mDay).show(); 
			}  
		});  
		if ((m_personid!=null) && (m_personid.length()>0) && 
				(!m_personid.equals(ConstantData.ADD_PERSON)))
		{
			fillData();				
		}else
		{
			m_personid = PersonProfileCache.generatePersonID(this);
			Bitmap newBitmap = URIConverter.getHeadFromAppSDCard(this, ConstantData.DEFAULT_HEAD_ICON_MALE,"","男");			
			iv_image.setImageBitmap(newBitmap);
		}
		
		spinnerTitle.setVisibility(View.VISIBLE);
		
		
		
	}
	
	private boolean fillData()
	{
		Dao<PersonProfile, Integer> profileDao;
		try {
			profileDao = getHelper(this).getPersonProfileDao();
			List<PersonProfile> pps = profileDao.queryForEq("personID", m_personid);
			if (pps.size()==1)
			{
				final PersonProfile pp = pps.get(0);
				txtName.setText((pp.getName()==null)?"":pp.getName());
				
				mSharelist = pp.getSharelist();

				if ((pp.getSex() != null)&&(pp.getSex().length()!=0))

				mSex = pp.getSex();
				
				if (mSex.equals("男"))
				{
					radioMale.setChecked(true);
					radioFemale.setChecked(false);
				}
				else
				{
					radioMale.setChecked(false);
					radioFemale.setChecked(true);
				}	

				Dao<UserToPersons, Integer> u2pDao = getHelper(this).getUserToPersonsDao();
				List<UserToPersons> u2ps = u2pDao.queryForEq("personID", m_personid);
				if (u2ps.size()==1)
				{
					mTitle = u2ps.get(0).getRetitle();
					mOldTitle = mTitle;
				}
				//if (pp.getTitle() != null)
				//	mTitle = pp.getTitle();				
				spinnerTitle.setSelection(ConstantData.titles.indexOf(mTitle));
				
				if (pp.getBirthYear() != 0)
				mYear = pp.getBirthYear();
				if (pp.getBirthMonth() != 0)
				mMonth = pp.getBirthMonth();
				if (pp.getBirthDay() != 0)
				mDay = pp.getBirthDay();
				

				txtWeight.setText(pp.getWeigth()!=null?String.valueOf(pp.getWeigth()):"65");
				txtHeight.setText(pp.getHeigth()!=null?String.valueOf(pp.getHeigth()):"165");				
				txtAllerge.setText((pp.getAllergy()==null)?"":pp.getAllergy());
				txtDisease.setText((pp.getDisease()==null)?"":pp.getDisease());
				txtLife.setText((pp.getLife()==null)?"":pp.getLife());
				txtEat.setText((pp.getEat()==null)?"":pp.getEat());
				txtIssues.setText((pp.getIssues()==null)?"":pp.getIssues());
				
				
				sellStartTime.setText(String.valueOf(mYear)+"-"+String.valueOf(mMonth)
						+"-"+String.valueOf(mDay));
				sellStartTime.setOnClickListener(new OnClickListener() {  
				
					@Override
					public void onClick(View v) {
						int year = pp.getBirthYear();
						int month = pp.getBirthMonth();
						int day = pp.getBirthDay();
						year = (year==0?1980:year);
						 new DatePickerDialog(ProfileActivity.this,  
				                    mDateSetListener,  
				                    year, month, 
				                    day).show(); 
					}  
				});  
			
				imageFileNameInOld = ( pp.getImageFileName()==null?"":pp.getImageFileName());
				Bitmap newBitmap = URIConverter.getHeadFromAppSDCard(this, pp.getImageFileName(),pp.getPersonID(),pp.getSex());			
				iv_image.setImageBitmap(newBitmap);
				
			}else
			{
				Toast.makeText(ProfileActivity.this,
						"读取用户信息失败,重复用户",
						Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	

	
	private String[] getAllTitle() {
		String[] stockArr = new String[ConstantData.titles.size()];
		return (String[]) ConstantData.titles.toArray(stockArr);
	}

	@Override  
	public boolean onCreateOptionsMenu(Menu menu) {  
	    menu.add(0, 102, menu.size()+1, "删除关注人");  
	    return super.onCreateOptionsMenu(menu);  
	}  
	private void deletePerson() {
		Dao<PersonProfile, Integer> profileDao;
		try {
			profileDao = getHelper(this).getPersonProfileDao();
			DeleteBuilder<PersonProfile, Integer> dbuilder = profileDao.deleteBuilder();
			dbuilder.where().eq("personID", m_personid);
			dbuilder.delete();
			
			Dao<HealthInputData, Integer> inputDao = 
					getHelper(this).getInputDataDao();
			DeleteBuilder<HealthInputData, Integer> dbuilder2 = 
					inputDao.deleteBuilder();
			dbuilder2.where().eq("personID", m_personid);
			dbuilder2.delete();
			
			Dao<UserToPersons, Integer> userToPDao = 
					getHelper(this).getUserToPersonsDao();
			DeleteBuilder<UserToPersons, Integer> dbuilder3 = 
					userToPDao.deleteBuilder();
			dbuilder3.where().eq("personID", m_personid);
			dbuilder3.delete();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Override  
    public boolean onOptionsItemSelected(MenuItem item) {  
        switch (item.getItemId()) {  
        case 102:  
        	new AlertDialog.Builder(this)
			.setTitle("删除")
			.setMessage("此人的所有信息将被删除？")
			.setPositiveButton("是",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int which) {
							deletePerson();

				        	final Intent intent = new Intent(ProfileActivity.this, HMBMainActivity.class);
				        	intent.setClass(ProfileActivity.this, HMBMainActivity.class);        	
							startActivity(intent);//
						}
						
						
					})
			.setNegativeButton("否", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int which) {
							
						}
					}).show();
            break;  
        }  
        return super.onOptionsItemSelected(item);  
    }  

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Bitmap newBitmap;
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
			case TAKE_PICTURE:
				//crop
				//startPhotoCrop(TAKE_PHOTE_TEMP_URI,getImageFileName(m_personid));
				//startCropWith3rd(TAKE_PHOTE_TEMP_URI,getImageFileName(m_personid));
				
				//no crop
				//newBitmap = decodeUriAsBitmap(TAKE_PHOTE_TEMP_URI,SCALE);
				//iv_image.setImageBitmap(newBitmap);

				startCropWith3rd(TAKE_PHOTE_TEMP_URI,getImageFileName(m_personid),true,"PNG",200,200);
				break;

			case CHOOSE_PICTURE:
				Uri originalUri = data.getData(); 
				//crop				
				//startPhotoCrop(originalUri,getImageFileName(m_personid));
				//startCropWith3rd(originalUri,getImageFileName(m_personid));
				
				//no crop
				newBitmap = decodeUriAsBitmap(originalUri,SCALE);			
				//iv_image.setImageBitmap(newBitmap);
				URIConverter.savePhotoToAppSDCard(this,newBitmap, TAKE_PHOTE_TEMP_FILENAME );//getImageFileName(m_personid)

				startCropWith3rd(TAKE_PHOTE_TEMP_URI,getImageFileName(m_personid),true,"PNG",200,200);
				break;
				
			case CROP_PHOTO_RESULT:
				//
				newBitmap = URIConverter.getHeadFromAppSDCard(this, getImageFileName(m_personid),"","男");			
				//upload to KuaiPan
				KuaiPanManager.getInstance(ProfileActivity.this).uploadFile(
						m_personid,
						URIConverter.getFilePathFromAppSDCard(this,getImageFileName(m_personid)),
						getImageFileName(m_personid));
				iv_image.setImageBitmap(newBitmap);
				ImageAdapter.getInstance(ProfileActivity.this).refresh();
				takeAPhoto = true;
				break;
			case ConstantData.CUSTOM_DIALOG_REQUST_ID:
				selectedDialog.onActivityResult(requestCode, resultCode, data);
				
				break;
			default:
				break;
			}
		}
	}
	
	
	@Override
	protected String getImageFileName(String m_personid2)
	{
		return m_personid2+".png";
	}
	

	
	private void saveProfile(Context context)
	{
		PersonProfile pp = new PersonProfile();
		
		pp.setPersonID(m_personid);
		
		pp.setName(txtName.getText().toString());
		pp.setSex(mSex);
		
	
		//pp.setTitle(mTitle);
		pp.setAge(Integer.valueOf(HealthDateUtil.getNowYear()) - mYear);
		pp.setBirthDay(mDay);
		pp.setBirthMonth(mMonth);
		pp.setBirthYear(mYear);
		if(txtWeight.getText().toString().equals("")) {
			pp.setWeigth(50.0);
		}else{
			pp.setWeigth(Double.valueOf(txtWeight.getText().toString()));
		}
		
		if(txtHeight.getText().toString().equals("")) {
			pp.setHeigth(165.0);
		}else {
			pp.setHeigth(Double.valueOf(txtHeight.getText().toString()));
		}
		String username = PersonProfileCache.getInstance(ProfileActivity.this)
				  .getUsername(ProfileActivity.this);
		if ((mSharelist == null)||(mSharelist.indexOf(username) < 0))
		{
			mSharelist = mSharelist + username;
		}
		pp.setSharelist(mSharelist);
		pp.setAllergy(txtAllerge.getText().toString());
		pp.setDisease(txtDisease.getText().toString());
		pp.setLife(txtLife.getText().toString());
		pp.setEat(txtEat.getText().toString());
		pp.setIssues(txtIssues.getText().toString());
		//create image for this person
		if (takeAPhoto)
		{
			pp.setImageFileName(getImageFileName(m_personid));				
		}else
		{
			if ((imageFileNameInOld.length()==0)||
					(imageFileNameInOld.equals(ConstantData.DEFAULT_HEAD_ICON_MALE)) ||
					(imageFileNameInOld.equals(ConstantData.DEFAULT_HEAD_ICON_FEMALE)))
			{
				if (mSex.equals("男"))
					pp.setImageFileName(ConstantData.DEFAULT_HEAD_ICON_MALE);
				else
					pp.setImageFileName(ConstantData.DEFAULT_HEAD_ICON_FEMALE);
			}else
				pp.setImageFileName(imageFileNameInOld);
			/*
			Bitmap bitmap = ImageTools.createTextBitmap(this, 236, 236, 6, 5, 5,
					mTitle,txtName.getText().toString());
			if (bitmap != null)
			{
				URIConverter.savePhotoToAppSDCard(bitmap, getImageFileName(m_personid));
				pp.setImageFileName(getImageFileName(m_personid));
			}
			
			else
				Log.e(LOG_TAG,"can't dynamically create an head image.");
			*/		
		}
		
		pp.setManualinput(ConstantData.ManualInputYes);
		
		// get our dao
		try {
			Dao<PersonProfile, Integer> profileDao = getHelper(this).getPersonProfileDao();
			List<PersonProfile> pps = profileDao.queryForEq("personID", m_personid);
			
			//DeleteBuilder<PersonProfile, Integer> dbuilder = profileDao.deleteBuilder();
			//dbuilder.where().eq("personID", m_personid);
			//dbuilder.delete();
			if (pps.size()>0)
			{
				pp.setId(pps.get(0).getId());
				profileDao.createOrUpdate(pp);
			}else
			{
				profileDao.create(pp);
			}
			//if head image changed refresh it.
			HMBMainActivity.refreshImage();
			PersonProfileUploadTask task = new PersonProfileUploadTask(context, m_personid, 
					new BasicCommCallback());
			HealthThreadPool.getInstance().execute(task);
			
			
			//save list
			Dao<UserToPersons, Integer> utopDao = getHelper(this).getUserToPersonsDao();
			List<UserToPersons> r = utopDao.queryBuilder().where().eq("personID", pp.getPersonID())

							.and().eq("username", PersonProfileCache.getInstance(ProfileActivity.this)
												  .getUsername(ProfileActivity.this)).query();
			UserToPersons up = null;
			if (mTitle.compareTo(mOldTitle)!=0)
			{
				if (r.size() == 0)
				{
					up = new UserToPersons();
					up.setServerID("zzz");
					up.setPersonID(pp.getPersonID());
					
					up.setUsername(PersonProfileCache.getInstance(ProfileActivity.this).getUsername(ProfileActivity.this));
					utopDao.create(up);
				}else
				{
					up = r.get(0);
					up.setRetitle(mTitle);
					utopDao.createOrUpdate(up);
				}
			}
			
			//create a new user to person record to make
			//other user get the updated profile
			UserToPersonsUpTask task2 = new UserToPersonsUpTask(					
					PersonProfileCache.getInstance(ProfileActivity.this).getUsername(ProfileActivity.this), 
					m_personid, 
					mTitle,"",
					this,
					new BasicCommCallback());
			HealthThreadPool.getInstance().execute(task2);
			
			Log.i(LOG_TAG, "created person profile.");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
    private DatePickerDialog.OnDateSetListener mDateSetListener =  
            new DatePickerDialog.OnDateSetListener() {  


				@Override
				public void onDateSet(DatePicker view, int year,
						int monthOfYear, int dayOfMonth) {
					mYear = year;  
                    String mm;  
                    String dd;  
					if(monthOfYear<=9)  
                    {  
                        mMonth = monthOfYear+1;  
                        mm="0"+mMonth;  
                    }  
                    else{  
                        mMonth = monthOfYear+1;  
                        mm=String.valueOf(mMonth);  
                        }  
					if(dayOfMonth<=9)  
                    {  
                        mDay = dayOfMonth;  
                        dd="0"+mDay;  
                    }  
                    else{  
                        mDay = dayOfMonth;  
                        dd=String.valueOf(mDay);  
                        }  
                    mDay = dayOfMonth;  
                    
                    sellStartTime.setText(String.valueOf(mYear)+"-"+mm+"-"+dd);  
                    
				}             
            };  
     
      
    // 隐藏手机键盘  
    private void hideIM(View edt){  
        try {  
             InputMethodManager im = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);  
             IBinder  windowToken = edt.getWindowToken();  
             if(windowToken != null) {  
                 im.hideSoftInputFromWindow(windowToken, 0);  
             }  
         } catch (Exception e) {  
             
         }  
     }  
    
	@Override
	public DatabaseHelper getHelper(Context context) {
		return DatabaseAccessImplementation.getInstance().getHelper(context);
	}
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		
		super.onWindowFocusChanged(hasFocus);
		if (hasFocus)
		{
			hideIM(this.getCurrentFocus());
		}
	}
	
}
